package org.csu.jpetstore_users_backend_pj.service.impl;

import org.csu.jpetstore_users_backend_pj.entity.Inventory;
import org.csu.jpetstore_users_backend_pj.mapper.InventoryMapper;
import org.csu.jpetstore_users_backend_pj.service.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class InventoryServiceImpl implements InventoryService {

    @Autowired
    private InventoryMapper inventoryMapper;

    @Override
    public int getInventoryByItemId(String itemId) {
        Inventory inventory = inventoryMapper.selectById(itemId);
        return inventory != null ? inventory.getQty() : 0;
    }

    @Override
    public boolean decreaseInventory(String itemId, int amount) {
        Inventory inventory = inventoryMapper.selectById(itemId);
        if (inventory != null && inventory.getQty() >= amount) {
            inventory.setQty(inventory.getQty() - amount);
            inventoryMapper.updateById(inventory);
            return true;
        }
        return false;
    }

    @Override
    public boolean increaseInventory(String itemId, int amount) {
        Inventory inventory = inventoryMapper.selectById(itemId);
        if (inventory != null) {
            inventory.setQty(inventory.getQty() + amount);
            inventoryMapper.updateById(inventory);
            return true;
        }
        return false;
    }
}

